﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Workbench.Core;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public class TreeViewMenu : TreeView, IMenu
    {
        public TreeViewMenu()
        {
            this.InitializeComponent();
        }

        #region Protected Methods

        protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
        {
            //execute the command associated with the clicked node
            ((IMenuItem)e.Node).Command.Execute();

            base.OnNodeMouseClick(e);
        }

        #endregion

        #region IMenu Members

        public void AddItem(IMenuItem item)
        {
            TreeNode node = item as TreeNode;
            if (node != null)
                this.Nodes.Add(node);
            else
                throw new ArgumentException("Cannot convert menu item to TreeNode");
        }

        #endregion

        #region IEnumerable<IMenuItem> Members

        public IEnumerator<IMenuItem> GetEnumerator()
        {
            return this.Nodes.OfType<IMenuItem>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // TreeViewMenu
            // 
            this.HideSelection = false;
            this.ResumeLayout(false);
        }
    }
}
